<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>pandas入门 | 杨一赫的博客</title><meta name="author" content="杨一赫"><meta name="copyright" content="杨一赫"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="第一章 数据结构简介 series创建 引入外部库 # !pip install pandas-profiling# !pip install --user pandas_profiling -i https:&#x2F;&#x2F;pypi.tuna.tsinghua.edu.cn&#x2F;simple!pip install --user dtale -i https:&#x2F;&#x2F;pypi.tuna.tsinghua.e">
<meta property="og:type" content="article">
<meta property="og:title" content="pandas入门">
<meta property="og:url" content="http://yang1he.gitee.io/2021/09/05/pandas%E5%85%A5%E9%97%A8/index.html">
<meta property="og:site_name" content="杨一赫的博客">
<meta property="og:description" content="第一章 数据结构简介 series创建 引入外部库 # !pip install pandas-profiling# !pip install --user pandas_profiling -i https:&#x2F;&#x2F;pypi.tuna.tsinghua.edu.cn&#x2F;simple!pip install --user dtale -i https:&#x2F;&#x2F;pypi.tuna.tsinghua.e">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img.freepik.com/premium-psd/plastic-individual-tablecloth-mockup-top-view_1332-10763.jpg?w=1480">
<meta property="article:published_time" content="2021-09-04T16:00:00.000Z">
<meta property="article:modified_time" content="2023-02-23T09:03:08.243Z">
<meta property="article:author" content="杨一赫">
<meta property="article:tag" content="-码农基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img.freepik.com/premium-psd/plastic-individual-tablecloth-mockup-top-view_1332-10763.jpg?w=1480"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://yang1he.gitee.io/2021/09/05/pandas%E5%85%A5%E9%97%A8/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?6b9e2c1c603a1d11d43a9822bdd68c2c";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: {"limitDay":90,"position":"top","messagePrev":"距离上次更新已经","messageNext":"天本文可能会过期"},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'pandas入门',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-02-23 17:03:08'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><script src="https://cdn.jsdelivr.net/npm/echarts@5.3.0/dist/echarts.min.js"></script><script src="https://cdn.jsdelivr.net/npm/echarts@4.7.0/map/js/china.min.js"></script><script src="node_modules/simplex-noise/simplex-noise.js"></script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="杨一赫的博客" type="application/atom+xml">
</head><body><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pace-js/themes/blue/pace-theme-minimal.min.css"/><script src="https://cdn.jsdelivr.net/npm/pace-js/pace.min.js"></script><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/favicon2.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about"><i class="fa-fw fa fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://img.freepik.com/premium-psd/plastic-individual-tablecloth-mockup-top-view_1332-10763.jpg?w=1480')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">杨一赫的博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about"><i class="fa-fw fa fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">pandas入门</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-09-04T16:00:00.000Z" title="发表于 2021-09-05 00:00:00">2021-09-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-02-23T09:03:08.243Z" title="更新于 2023-02-23 17:03:08">2023-02-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/pandas%E7%AC%94%E8%AE%B0/">pandas笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="pandas入门"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="第一章">第一章</h2>
<h3 id="数据结构简介">数据结构简介</h3>
<h3 id="series创建">series创建</h3>
<p>引入外部库</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment"># !pip install pandas-profiling</span></span><br><span class="line"><span class="comment"># !pip install --user pandas_profiling -i https://pypi.tuna.tsinghua.edu.cn/simple</span></span><br><span class="line">!pip install --user dtale -i https://pypi.tuna.tsinghua.edu.cn/simple</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd </span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br></pre></td></tr></table></figure>
<h4 id="从数组中创建">从数组中创建</h4>
<p>series是一个一维标记数组，能够保存任何数据类型,轴标签统称为索引，创建series的基本方法是调用</p>
<p>s=pd.Series(data,i) data可以是字典、数组、数字
如果data是<strong>数组</strong>， -
<strong>索引必须与数据的长度相同</strong> data[0, ..., len(data) - 1] -
**索引可以有重复,但重复索引不意味着它的值也重复</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s=pd.Series(np.random.randn(<span class="number">5</span>),index=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>])</span><br><span class="line">s</span><br></pre></td></tr></table></figure>
<pre><code>---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

Input In [1], in &lt;cell line: 1&gt;()
----&gt; 1 s=pd.Series(np.random.randn(5),index=[&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;])
      2 s


NameError: name &#39;pd&#39; is not defined</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s=pd.Series(np.random.randn(<span class="number">5</span>),index=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;d&#x27;</span>])</span><br><span class="line">s</span><br></pre></td></tr></table></figure>
<pre><code>---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

Input In [1], in &lt;cell line: 1&gt;()
----&gt; 1 s=pd.Series(np.random.randn(5),index=[&#39;a&#39;,&#39;b&#39;,&#39;d&#39;,&#39;d&#39;,&#39;d&#39;])
      2 s


NameError: name &#39;pd&#39; is not defined</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s.index</span><br></pre></td></tr></table></figure>
<p>不填index，默认分配索引</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">pd.Series(np.random.randn(<span class="number">5</span>))</span><br></pre></td></tr></table></figure>
<h4 id="series-可以从字典中实例化">series 可以从字典中实例化</h4>
<ul>
<li>特点1 series按照字典顺序排序</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d=<span class="built_in">dict</span>(b=<span class="number">1</span>,a=<span class="number">0</span>,c=<span class="number">2</span>)</span><br><span class="line">pd.Series(d)</span><br></pre></td></tr></table></figure>
<ul>
<li>特点2 字典构建索引，如果索引标签对应字典的key将会自动做匹配
其实从结果上也能看出 - 索引值可以有重复，在字典匹配的时候， -
如果匹配不上就会出现空值</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d=<span class="built_in">dict</span>(b=<span class="number">1</span>,a=<span class="number">0</span>,c=<span class="number">2</span>)</span><br><span class="line">pd.Series(d,index=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>])</span><br></pre></td></tr></table></figure>
<p>NaN是pandas的标准缺失数据标记</p>
<h4 id="从单个数值创建series">从单个数值创建series</h4>
<p>标量值，必须提供索引，将重复该值，以匹配索引长度</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">pd.Series(<span class="number">5</span>,index=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>])</span><br></pre></td></tr></table></figure>
<h4 id="索引的切片操作">索引的切片操作</h4>
<ul>
<li><p>简单切片</p></li>
<li><p>简单切片</p></li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s[<span class="number">0</span>] </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">0在这里表示的位置，series切片，与字典规则一致，优先看索引，</span></span><br><span class="line"><span class="string">没有找到有关索引，会看是否有数字对应的位置</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line">s[:<span class="number">3</span>]</span><br></pre></td></tr></table></figure>
<ul>
<li>条件切片</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s[s&gt;s[<span class="number">1</span>:<span class="number">3</span>].median()]</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s[[<span class="number">4</span>,<span class="number">3</span>,<span class="number">1</span>]]</span><br></pre></td></tr></table></figure>
<ul>
<li>取指数或对数 （取对数要大于0否则会报错）</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">np.exp(s)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s=pd.Series([<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>])</span><br><span class="line">np.log(s)</span><br><span class="line">s</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s.dtype</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#### series条件判断-基于索引</span></span><br><span class="line">    + 和字典一样，可以判断</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="number">2</span> <span class="keyword">in</span> s</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="string">&quot;m&quot;</span> <span class="keyword">in</span> s</span><br></pre></td></tr></table></figure>
<p>这里和字典一样，可以用get方法，使得查不到也不会报错
或者get(查找对象,设定一个找不到的返回值)</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s.get(<span class="number">1</span>) <span class="comment">#能找到</span></span><br><span class="line">s.get(<span class="number">11</span>) <span class="comment">#找不到也不会报错</span></span><br></pre></td></tr></table></figure>
<h4 id="series-矢量化操作">series 矢量化操作</h4>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d=<span class="built_in">dict</span>(b=<span class="number">1</span>,a=<span class="number">0</span>,c=<span class="number">2</span>)</span><br><span class="line">s=pd.Series(d)</span><br><span class="line">s</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s+s</span><br><span class="line"></span><br><span class="line">s*s</span><br></pre></td></tr></table></figure>
<p>series矢量操作可以基于series标签进行匹配计算 - [x]
只取并集，缺失值会na掉 - [x] 删缺失值用dropna,但是有个小细节 -
dropna会算到运算先后顺序中，所以前面的加减乘除要加括号表示优先级</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s_match=pd.Series([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],index=[<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>,<span class="string">&quot;d&quot;</span>])</span><br><span class="line">s_match</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s*s_match.dropna()<span class="comment"># 错误方法，这样删没用</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">(s*s_match).dropna()</span><br></pre></td></tr></table></figure>
<h4 id="series的名称属性name">series的名称属性name</h4>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 增名</span></span><br><span class="line">s=pd.Series(np.random.randn(<span class="number">5</span>),name=<span class="string">&#x27;something&#x27;</span>)</span><br><span class="line">s</span><br></pre></td></tr></table></figure>
<p>series名称查询</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s.name</span><br></pre></td></tr></table></figure>
<p>series name改</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s.rename(<span class="string">&#x27;different&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="dataframe创建">dataframe创建</h3>
<p>DataFrame
是一种二维标记数据结构，其中包含可能不同类型的列。您可以将其视为电子表格或
SQL 表，或者 Series
对象的字典。它通常是最常用的熊猫对象。与序列一样，DataFrame接受许多不同类型的输入：
- 1D ndarrays、列表、字典或系列的字典 - 2-D numpy.ndarray - 结构化或记录
ndarray - 一个Series - 另一个DataFrame - <strong>一个字典</strong> -
列表字典</p>
<h4 id="基于字典生成dataframe">基于字典生成DataFrame</h4>
<p>字典可以是以下几种 - d={ 列名1：{行名1，对应的表值}，
列名2：{行名2，对应的表值} } - d={ 列名1：表值， 列名2：表值
}#这种要额外设定DataFrame的index,否则会报错 所以
，字典自动转化为dataFrame最多两层</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&quot;one&quot;</span>: [<span class="number">1</span>,<span class="number">3</span>],</span><br><span class="line">    <span class="string">&quot;two&quot;</span>: <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line">df=pd.DataFrame(d,index=[<span class="number">0</span>,<span class="number">1</span>]) </span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d1 = &#123;</span><br><span class="line">    <span class="string">&quot;one&quot;</span>: pd.Series([<span class="number">1.0</span>, <span class="number">2.0</span>, <span class="number">3.0</span>], index=[<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>],name=<span class="string">&quot;yang&quot;</span>),</span><br><span class="line">    <span class="string">&quot;two&quot;</span>: pd.Series([<span class="number">1.0</span>, <span class="number">2.0</span>, <span class="number">3.0</span>, <span class="number">4.0</span>], index=[<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>],name=<span class="string">&#x27;yi&#x27;</span>),</span><br><span class="line">&#125;</span><br><span class="line">df1=pd.DataFrame(d1)</span><br><span class="line">df1</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;one&quot;</span>: [<span class="number">1</span>,<span class="number">3</span>],</span><br><span class="line">    <span class="string">&quot;two&quot;</span>: [<span class="number">2</span>,<span class="number">1</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d2=&#123;</span><br><span class="line">    <span class="string">&#x27;a&#x27;</span>:&#123;<span class="string">&#x27;ina&#x27;</span>:<span class="number">1</span>&#125;,</span><br><span class="line">    <span class="string">&#x27;b&#x27;</span>:&#123;<span class="string">&#x27;inb&#x27;</span>:<span class="number">2</span>&#125;</span><br><span class="line">&#125;</span><br><span class="line">df2=pd.DataFrame(d2)</span><br><span class="line">df2</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&quot;one&quot;</span>: [<span class="number">1</span>,<span class="number">3</span>],</span><br><span class="line">    <span class="string">&quot;two&quot;</span>: <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line">df=pd.DataFrame(d,index=[<span class="number">0</span>,<span class="number">1</span>]) </span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d3=&#123;</span><br><span class="line">    <span class="string">&#x27;a&#x27;</span>:&#123;<span class="string">&#x27;ina&#x27;</span>:&#123;<span class="string">&#x27;inaa&#x27;</span>:<span class="number">1</span>&#125;&#125;,</span><br><span class="line">    <span class="string">&#x27;b&#x27;</span>:&#123;<span class="string">&#x27;inb&#x27;</span>:&#123;<span class="string">&#x27;inb&#x27;</span>:<span class="number">2</span>&#125;&#125;</span><br><span class="line">&#125;</span><br><span class="line">df3=pd.DataFrame(d3)</span><br><span class="line">df3</span><br></pre></td></tr></table></figure>
<p>字典除了index,每列是columns属性 <mark>colums有以下特点</mark> 1.
和index一样，<mark>可以重复,但是设定的columns要和列数一致</mark> 1.
可以在字典导入时与字典第一层的键名进行匹配,匹配不到会Na -
当然,<mark>DataFrame不仅匹配键名,有index部分,也会匹配index</mark> 1.
当一组特定的列与数据字典一起传递时，传递的列将覆盖字典中的键,也就是dataframe中的键具有优先级</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&quot;one&quot;</span>: pd.Series([<span class="number">1.0</span>, <span class="number">2.0</span>, <span class="number">3.0</span>], index=[<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>]),</span><br><span class="line">    <span class="string">&quot;two&quot;</span>: pd.Series([<span class="number">1.0</span>, <span class="number">2.0</span>, <span class="number">3.0</span>, <span class="number">4.0</span>], index=[<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>]),</span><br><span class="line">&#125;</span><br><span class="line">pd.DataFrame(d, index=[<span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;a&#x27;</span>], columns=[<span class="string">&#x27;one&#x27;</span>, <span class="string">&#x27;two&#x27;</span>])</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&quot;one&quot;</span>: <span class="number">1</span>,</span><br><span class="line">    <span class="string">&quot;two&quot;</span>: <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line">df=pd.DataFrame(d, index=[<span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;a&#x27;</span>], columns=[<span class="string">&#x27;one&#x27;</span>, <span class="string">&#x27;one&#x27;</span>])</span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&quot;one&quot;</span>: [<span class="number">1</span>,<span class="number">3</span>],</span><br><span class="line">    <span class="string">&quot;two&quot;</span>: <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line">df=pd.DataFrame(d,index=[<span class="number">0</span>,<span class="number">1</span>]) </span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<h4 id="dataframe查询index和colums">dataframe查询index和colums</h4>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.index</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.columns</span><br></pre></td></tr></table></figure>
<h4 id="从列表或者ndarray">从列表或者ndarray</h4>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">data = np.zeros((<span class="number">2</span>,), dtype=[(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;i4&quot;</span>), (<span class="string">&quot;B&quot;</span>, <span class="string">&quot;f4&quot;</span>), (<span class="string">&quot;C&quot;</span>, <span class="string">&quot;a10&quot;</span>)])</span><br><span class="line">data</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&quot;one&quot;</span>: pd.Series([<span class="number">1.0</span>, <span class="number">2.0</span>, <span class="number">3.0</span>], index=[<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>]),</span><br><span class="line">    <span class="string">&quot;two&quot;</span>:&#123;<span class="number">1</span>:<span class="number">1</span>,<span class="number">2</span>:<span class="number">2</span>&#125;</span><br><span class="line">&#125;</span><br><span class="line">pd.DataFrame(d, index=[<span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,<span class="number">1</span>], columns=[<span class="string">&#x27;one&#x27;</span>, <span class="string">&#x27;two&#x27;</span>])</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">data[:] = [(<span class="number">1</span>, <span class="number">2.0</span>, <span class="string">&quot;Hello&quot;</span>), (<span class="number">2</span>, <span class="number">3.0</span>, <span class="string">&quot;World&quot;</span>)]</span><br><span class="line">pd.DataFrame(data)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">pd.DataFrame(data,index=[<span class="string">&#x27;one&#x27;</span>,<span class="string">&#x27;two&#x27;</span>],columns=[<span class="string">&#x27;A&#x27;</span>,<span class="string">&#x27;B&#x27;</span>,<span class="string">&#x27;C&#x27;</span>])</span><br></pre></td></tr></table></figure>
<h4 id="从列表字典导入">从列表字典导入</h4>
<ul>
<li>特点就是列表中每个字典第一层键名成为列名,自动生成index</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">data2 = [&#123;<span class="string">&quot;a&quot;</span>: <span class="number">1</span>, <span class="string">&quot;b&quot;</span>: <span class="number">2</span>&#125;, &#123;<span class="string">&quot;a&quot;</span>: <span class="number">5</span>, <span class="string">&quot;b&quot;</span>: <span class="number">10</span>, <span class="string">&quot;c&quot;</span>: <span class="number">20</span>&#125;]</span><br><span class="line">pd.DataFrame(data2)</span><br></pre></td></tr></table></figure>
<h4 id="多索引框架__从元祖字典中导入">多索引框架__从元祖字典中导入</h4>
<p>您可以通过传递元组字典来自动创建多索引框架</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">pd.DataFrame(</span><br><span class="line">    &#123;</span><br><span class="line">        (<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>): &#123;(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>): <span class="number">1</span>, (<span class="string">&quot;A&quot;</span>, <span class="string">&quot;C&quot;</span>): <span class="number">2</span>&#125;,</span><br><span class="line">        (<span class="string">&quot;a&quot;</span>, <span class="string">&quot;a&quot;</span>): &#123;(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;C&quot;</span>): <span class="number">3</span>, (<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>): <span class="number">4</span>&#125;,</span><br><span class="line">        (<span class="string">&quot;a&quot;</span>, <span class="string">&quot;c&quot;</span>): &#123;(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>): <span class="number">5</span>, (<span class="string">&quot;A&quot;</span>, <span class="string">&quot;C&quot;</span>): <span class="number">6</span>&#125;,</span><br><span class="line">        (<span class="string">&quot;b&quot;</span>, <span class="string">&quot;a&quot;</span>): &#123;(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;C&quot;</span>): <span class="number">7</span>, (<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>): <span class="number">8</span>&#125;,</span><br><span class="line">        (<span class="string">&quot;b&quot;</span>, <span class="string">&quot;b&quot;</span>): &#123;(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;D&quot;</span>): <span class="number">9</span>, (<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>): <span class="number">10</span>&#125;,</span><br><span class="line">    &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<h4 id="备用构造函数">备用构造函数</h4>
<p>Dataframe.from_dict -
特点<mark>就是多了个参数,把键</mark>名设定为行标签还是列标签 -
也就是<mark>orient='index'或者orient='columns'</mark></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&quot;one&quot;</span>: [<span class="number">1</span>,<span class="number">3</span>],</span><br><span class="line">    <span class="string">&quot;two&quot;</span>: [<span class="number">2</span>,<span class="number">1</span>]</span><br><span class="line">&#125;</span><br><span class="line">df.from_dict(d)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.from_dict(d,orient=<span class="string">&#x27;index&#x27;</span>,columns=[<span class="number">0</span>,<span class="number">1</span>])</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.from_dict(d,orient=<span class="string">&#x27;columns&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>DataFrame.from_records</p>
<p>DataFrame.from_records采用元组列表或具有结构化 dtype 的
ndarray。它的工作方式类似于普通构造函数，不同之处在于生成的 DataFrame
索引可能是结构化 dtype 的特定字段。例如：DataFrame - 先搁置</p>
<h3 id="列选择删除添加">列选择,删除,添加</h3>
<p><em>将 DataFrame 视为类似索引的 Series
对象的字典。获取、设置和删除列的语法与类似的 dict 操作相同</em></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">data2 = [&#123;<span class="string">&quot;a&quot;</span>: <span class="number">1</span>, <span class="string">&quot;b&quot;</span>: <span class="number">2</span>&#125;, &#123;<span class="string">&quot;a&quot;</span>: <span class="number">5</span>, <span class="string">&quot;b&quot;</span>: <span class="number">10</span>, <span class="string">&quot;c&quot;</span>: <span class="number">20</span>&#125;]</span><br><span class="line">df=pd.DataFrame(data2)</span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df[<span class="string">&#x27;a&#x27;</span>]</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df[<span class="string">&#x27;flag&#x27;</span>]=df[<span class="string">&#x27;a&#x27;</span>]&gt;<span class="number">2</span> <span class="comment">#还是可以创建0,1虚拟变量的</span></span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df[<span class="string">&quot;three&quot;</span>] = df[<span class="string">&quot;a&quot;</span>] * df[<span class="string">&quot;b&quot;</span>]  <span class="comment">#两列相乘</span></span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">del</span> df[<span class="string">&#x27;c&#x27;</span>] <span class="comment">#像字典一样删除或弹出列</span></span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">three=df.pop(<span class="string">&#x27;three&#x27;</span>)</span><br><span class="line">three</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df[<span class="string">&#x27;foo&#x27;</span>]=<span class="string">&#x27;bar&#x27;</span><span class="comment">#插入标量值自动填充列</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df[<span class="string">&#x27;bj&#x27;</span>]=df[<span class="string">&#x27;a&#x27;</span>][<span class="number">0</span>:<span class="number">1</span>] <span class="comment">#插入与数据帧没有相同索引的序列时，它将符合数据帧的索引</span></span><br></pre></td></tr></table></figure>
<p>可以用insert描述插入列中特定位置,insert(位置,列名称.列值)</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.insert(<span class="number">1</span>,<span class="string">&#x27;bar&#x27;</span>,df[<span class="string">&#x27;a&#x27;</span>])</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df</span><br></pre></td></tr></table></figure>
<h4 id="dataframe的assign方法">DataFrame的assign方法()</h4>
<ul>
<li><p>可以直接矢量运算得出ces</p></li>
<li><p>也可以用assign设计新列,好处是可以批量生成新列</p></li>
<li><p>也支持lamdba函数</p></li>
<li><p>assign内部比如有两个新列,那第一个创建的新列可以被第二个引用
assign的顺序也有了价值</p></li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="number">0</span>: [<span class="number">5.1</span>, <span class="number">3.5</span>, <span class="number">1.4</span>, <span class="number">0.2</span>],</span><br><span class="line">    <span class="number">1</span>: [<span class="number">4.9</span>, <span class="number">3.0</span>, <span class="number">1.4</span>, <span class="number">0.2</span>],</span><br><span class="line">    <span class="number">2</span>: [<span class="number">4.7</span>, <span class="number">3.2</span>, <span class="number">1.3</span>, <span class="number">0.2</span>],</span><br><span class="line">    <span class="number">3</span>: [<span class="number">4.6</span>, <span class="number">3.1</span>, <span class="number">1.5</span>, <span class="number">0.2</span>]</span><br><span class="line">&#125;</span><br><span class="line">d</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi=df.from_dict(d,orient=<span class="string">&#x27;index&#x27;</span>,columns=[<span class="string">&#x27;SepalLength&#x27;</span>,<span class="string">&#x27;SepalWidth&#x27;</span>,<span class="string">&#x27;PetalLength&#x27;</span>,<span class="string">&#x27;PetalWidth&#x27;</span>])</span><br><span class="line">df_assi[<span class="string">&#x27;Name&#x27;</span>]=<span class="string">&#x27;Iris-setosa&#x27;</span></span><br><span class="line">df_assi</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi[<span class="string">&#x27;ces&#x27;</span>]=df_assi[<span class="string">&#x27;SepalLength&#x27;</span>]/df_assi[<span class="string">&#x27;SepalWidth&#x27;</span>]</span><br><span class="line"><span class="comment"># iris.assign(sepal_ratio=iris[&quot;SepalWidth&quot;] / iris[&quot;SepalLength&quot;])</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi.assign(sepal_ratio=df_assi[<span class="string">&#x27;SepalLength&#x27;</span>]/df_assi[<span class="string">&#x27;SepalWidth&#x27;</span>])</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi.assign(ratio=<span class="keyword">lambda</span> x:(x[<span class="string">&#x27;SepalLength&#x27;</span>]/x[<span class="string">&#x27;SepalWidth&#x27;</span>]))</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#以下举例,先筛数据,再创造新列</span></span><br><span class="line">df_assi.query(<span class="string">&quot;SepalLength &lt;5&quot;</span>).assign(</span><br><span class="line">    SepalRatio=<span class="keyword">lambda</span> x: x.SepalWidth / x.SepalLength,</span><br><span class="line">    PetalRatio=<span class="keyword">lambda</span> x: x.PetalWidth / x.PetalLength</span><br><span class="line">).plot(kind=<span class="string">&quot;scatter&quot;</span>, x=<span class="string">&quot;SepalRatio&quot;</span>, y=<span class="string">&quot;PetalRatio&quot;</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">dfa = pd.DataFrame(&#123;<span class="string">&quot;A&quot;</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="string">&quot;B&quot;</span>: [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]&#125;)</span><br><span class="line">dfa.assign(C=<span class="keyword">lambda</span> x: x[<span class="string">&quot;A&quot;</span>] + x[<span class="string">&quot;B&quot;</span>], D=<span class="keyword">lambda</span> x: x[<span class="string">&quot;A&quot;</span>] + x[<span class="string">&quot;C&quot;</span>])</span><br></pre></td></tr></table></figure>
<h4 id="索引选择">索引选择</h4>
<table>
<thead>
<tr class="header">
<th>操作</th>
<th>语法</th>
<th>结果</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>选择列</td>
<td>df[col]</td>
<td>系列</td>
</tr>
<tr class="even">
<td><mark>按标签选择行</mark></td>
<td>df.loc[label]</td>
<td>系列</td>
</tr>
<tr class="odd">
<td><mark>按整数位置选择行</mark></td>
<td>df.iloc[loc]</td>
<td>系列</td>
</tr>
<tr class="even">
<td>对行进行切片</td>
<td>df[5:10]</td>
<td>数据帧</td>
</tr>
<tr class="odd">
<td>按布尔向量选择行</td>
<td>df[bool_vec]</td>
<td>数据帧</td>
</tr>
</tbody>
</table>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi.loc[<span class="number">1</span>].rename(<span class="string">&#x27;xin&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="dataframe之间加减合并">dataframe之间加减合并</h4>
<pre><code>- dataframe之间的加号不代表表格合并,代表矢量相加
- dataframe之间的相加也是取并集
-dataframe匹配行列索引</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line">df = pd.DataFrame(np.random.randn(<span class="number">10</span>, <span class="number">4</span>), columns=[<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>, <span class="string">&quot;D&quot;</span>])</span><br><span class="line">df2 = pd.DataFrame(np.random.randn(<span class="number">7</span>, <span class="number">3</span>), columns=[<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>])</span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }
    
    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>
</th>
<th>
A
</th>
<th>
B
</th>
<th>
C
</th>
<th>
D
</th>
</tr>
</thead>
<tbody>
<tr>
<th>
0
</th>
<td>
-1.112220
</td>
<td>
0.560869
</td>
<td>
0.179151
</td>
<td>
-0.251862
</td>
</tr>
<tr>
<th>
1
</th>
<td>
0.643623
</td>
<td>
0.966182
</td>
<td>
0.572348
</td>
<td>
1.550963
</td>
</tr>
<tr>
<th>
2
</th>
<td>
1.459080
</td>
<td>
-1.032543
</td>
<td>
1.309320
</td>
<td>
0.167273
</td>
</tr>
<tr>
<th>
3
</th>
<td>
0.456485
</td>
<td>
-0.015946
</td>
<td>
0.868748
</td>
<td>
-1.638820
</td>
</tr>
<tr>
<th>
4
</th>
<td>
-0.895094
</td>
<td>
1.276620
</td>
<td>
-1.282403
</td>
<td>
-1.064329
</td>
</tr>
<tr>
<th>
5
</th>
<td>
-1.680454
</td>
<td>
-0.231054
</td>
<td>
-1.085148
</td>
<td>
1.097739
</td>
</tr>
<tr>
<th>
6
</th>
<td>
-0.642960
</td>
<td>
-1.757372
</td>
<td>
-2.205203
</td>
<td>
0.002523
</td>
</tr>
<tr>
<th>
7
</th>
<td>
2.369471
</td>
<td>
-1.186986
</td>
<td>
0.269402
</td>
<td>
1.799190
</td>
</tr>
<tr>
<th>
8
</th>
<td>
-2.374937
</td>
<td>
-1.917268
</td>
<td>
1.099931
</td>
<td>
-0.048572
</td>
</tr>
<tr>
<th>
9
</th>
<td>
-0.744104
</td>
<td>
0.027437
</td>
<td>
1.428858
</td>
<td>
-0.296034
</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df2</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df+df2</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.iloc[<span class="number">0</span>].rename(<span class="number">1</span>)</span><br></pre></td></tr></table></figure>
<pre><code>A   -1.112220
B    0.560869
C    0.179151
D   -0.251862
Name: 1, dtype: float64</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df - df.iloc[<span class="number">0</span>].rename(<span class="number">1</span>)</span><br></pre></td></tr></table></figure>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }
    
    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>
</th>
<th>
A
</th>
<th>
B
</th>
<th>
C
</th>
<th>
D
</th>
</tr>
</thead>
<tbody>
<tr>
<th>
0
</th>
<td>
0.000000
</td>
<td>
0.000000
</td>
<td>
0.000000
</td>
<td>
0.000000
</td>
</tr>
<tr>
<th>
1
</th>
<td>
1.755843
</td>
<td>
0.405313
</td>
<td>
0.393197
</td>
<td>
1.802825
</td>
</tr>
<tr>
<th>
2
</th>
<td>
2.571300
</td>
<td>
-1.593412
</td>
<td>
1.130168
</td>
<td>
0.419135
</td>
</tr>
<tr>
<th>
3
</th>
<td>
1.568705
</td>
<td>
-0.576815
</td>
<td>
0.689597
</td>
<td>
-1.386958
</td>
</tr>
<tr>
<th>
4
</th>
<td>
0.217126
</td>
<td>
0.715751
</td>
<td>
-1.461554
</td>
<td>
-0.812467
</td>
</tr>
<tr>
<th>
5
</th>
<td>
-0.568235
</td>
<td>
-0.791923
</td>
<td>
-1.264300
</td>
<td>
1.349601
</td>
</tr>
<tr>
<th>
6
</th>
<td>
0.469260
</td>
<td>
-2.318241
</td>
<td>
-2.384354
</td>
<td>
0.254385
</td>
</tr>
<tr>
<th>
7
</th>
<td>
3.481691
</td>
<td>
-1.747855
</td>
<td>
0.090251
</td>
<td>
2.051052
</td>
</tr>
<tr>
<th>
8
</th>
<td>
-1.262717
</td>
<td>
-2.478137
</td>
<td>
0.920779
</td>
<td>
0.203290
</td>
</tr>
<tr>
<th>
9
</th>
<td>
0.368115
</td>
<td>
-0.533432
</td>
<td>
1.249706
</td>
<td>
-0.044172
</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi=df.from_dict(d,orient=<span class="string">&#x27;index&#x27;</span>,columns=[<span class="string">&#x27;SepalLength&#x27;</span>,<span class="string">&#x27;SepalWidth&#x27;</span>,<span class="string">&#x27;PetalLength&#x27;</span>,<span class="string">&#x27;PetalWidth&#x27;</span>])</span><br><span class="line">df_assi[<span class="string">&#x27;Name&#x27;</span>]=<span class="string">&#x27;Iris-setosa&#x27;</span></span><br><span class="line">df_assi</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi[<span class="string">&#x27;ces&#x27;</span>]=df_assi[<span class="string">&#x27;SepalLength&#x27;</span>]/df_assi[<span class="string">&#x27;SepalWidth&#x27;</span>]</span><br><span class="line"><span class="comment"># iris.assign(sepal_ratio=iris[&quot;SepalWidth&quot;] / iris[&quot;SepalLength&quot;])</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi.assign(sepal_ratio=df_assi[<span class="string">&#x27;SepalLength&#x27;</span>]/df_assi[<span class="string">&#x27;SepalWidth&#x27;</span>])</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi.assign(ratio=<span class="keyword">lambda</span> x:(x[<span class="string">&#x27;SepalLength&#x27;</span>]/x[<span class="string">&#x27;SepalWidth&#x27;</span>]))</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#以下举例,先筛数据,再创造新列</span></span><br><span class="line">df_assi.query(<span class="string">&quot;SepalLength &lt;5&quot;</span>).assign(</span><br><span class="line">    SepalRatio=<span class="keyword">lambda</span> x: x.SepalWidth / x.SepalLength,</span><br><span class="line">    PetalRatio=<span class="keyword">lambda</span> x: x.PetalWidth / x.PetalLength</span><br><span class="line">).plot(kind=<span class="string">&quot;scatter&quot;</span>, x=<span class="string">&quot;SepalRatio&quot;</span>, y=<span class="string">&quot;PetalRatio&quot;</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">dfa = pd.DataFrame(&#123;<span class="string">&quot;A&quot;</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="string">&quot;B&quot;</span>: [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]&#125;)</span><br><span class="line">dfa.assign(C=<span class="keyword">lambda</span> x: x[<span class="string">&quot;A&quot;</span>] + x[<span class="string">&quot;B&quot;</span>], D=<span class="keyword">lambda</span> x: x[<span class="string">&quot;A&quot;</span>] + x[<span class="string">&quot;C&quot;</span>])</span><br></pre></td></tr></table></figure>
<h4 id="索引选择-1">索引选择</h4>
<table>
<thead>
<tr class="header">
<th>操作</th>
<th>语法</th>
<th>结果</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>选择列</td>
<td>df[col]</td>
<td>系列</td>
</tr>
<tr class="even">
<td><mark>按标签选择行</mark></td>
<td>df.loc[label]</td>
<td>系列</td>
</tr>
<tr class="odd">
<td><mark>按整数位置选择行</mark></td>
<td>df.iloc[loc]</td>
<td>系列</td>
</tr>
<tr class="even">
<td>对行进行切片</td>
<td>df[5:10]</td>
<td>数据帧</td>
</tr>
<tr class="odd">
<td>按布尔向量选择行</td>
<td>df[bool_vec]</td>
<td>数据帧</td>
</tr>
</tbody>
</table>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_assi.loc[<span class="number">1</span>].rename(<span class="string">&#x27;xin&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="dataframe之间加减合并-1">dataframe之间加减合并</h4>
<pre><code>- dataframe之间的加号不代表表格合并,代表矢量相加
- dataframe之间的相加也是取并集
-dataframe匹配行列索引</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df = pd.DataFrame(np.random.randn(<span class="number">10</span>, <span class="number">4</span>), columns=[<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>, <span class="string">&quot;D&quot;</span>])</span><br><span class="line">df2 = pd.DataFrame(np.random.randn(<span class="number">7</span>, <span class="number">3</span>), columns=[<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>])</span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df2</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df+df2</span><br></pre></td></tr></table></figure>
<p>DataFrame 和 Series 之间执行操作时，默认行为是对齐 DataFrame 列上的
Series 索引(这里是series的name)，从而按行运算</p>
<ul>
<li>非常重要的一点,就是比如做行列式运算,第二行减去第一行,在dataframe不能直接<code>df - df.iloc[0].rename(1)</code>
<ul>
<li>因为这个操作实际上会计算两步
<ul>
<li>第一步是把第一行减去了自身</li>
<li>第二步是让第二行减去第一行
-所以哪怕是一个最简的的,第二行减去第一行,轻易别用rename来减</li>
</ul></li>
</ul></li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#法一错误写法</span></span><br><span class="line">df - df.iloc[<span class="number">0</span>].rename(<span class="number">1</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#法一正确写法</span></span><br><span class="line">df - df.iloc[<span class="number">0</span>].rename(<span class="number">1</span>)+df.iloc[<span class="number">0</span>]</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#法二在元数据df上更改.可以直接...</span></span><br><span class="line">df.iloc[<span class="number">1</span>]=df.iloc[<span class="number">1</span>] - df.iloc[<span class="number">0</span>]</span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">-  法三,pandas设置了详细的函数,下章讲</span><br></pre></td></tr></table></figure>
<h4
id="dataframe支持标量布尔运算转置">dataframe支持标量,布尔运算,转置</h4>
<p>标量预算举例 - <code>df*5+2</code> - <code>df**4</code> -
<code>np.exp(df)</code> - <code>np.reminder(ser1,ser2)</code>
#求1除2的余数,并用缺失值填充非重叠值,会自动匹配标签</p>
<p>布尔运算举例 - <mark>符号运算</mark> 先转化为布尔才能符号运算 - &amp;
且 - | 或 - ^ 对应位置相等取True,不相等取False - - 取反 -
<mark>函数运算</mark>#可能比较常用，不用转布尔</p>
<table>
<thead>
<tr class="header">
<th>方法</th>
<th>英文全称</th>
<th>用途</th>
<th>举例</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>eq</td>
<td>equal to</td>
<td>等于</td>
<td></td>
</tr>
<tr class="even">
<td>ne</td>
<td>not equal to 不等于</td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>lt</td>
<td>less than</td>
<td>小于</td>
<td></td>
</tr>
<tr class="even">
<td>gt</td>
<td>greater than</td>
<td>大于</td>
<td><code>df.gt(df2)</code></td>
</tr>
<tr class="odd">
<td>le</td>
<td>less than or equal to</td>
<td>小于等于</td>
<td></td>
</tr>
<tr class="even">
<td>ge</td>
<td>greater than or equal to</td>
<td>大于等于</td>
<td></td>
</tr>
</tbody>
</table>
<ul>
<li>布尔约简,布尔运算更聚合的汇总
<ul>
<li>空、any（）、all（） 和 bool（）
来提供一种布尔结果汇总的方法eg<code>(df &gt; 0).all()</code>
<ul>
<li><code>(df &gt; 0).any().any()</code>这样可以多层聚合</li>
</ul></li>
<li>通过空属性测试 pandas 对象是否为空<code>df.empty</code></li>
</ul></li>
<li>补充：布尔方法
<ul>
<li>None有一个真值False， 数字 zero( 0) –
整数、浮点数和复数表示0并且都有一个真值False，
所有空的可迭代对象（如列表、元组和字符串）的真值为False。
也就是说，所有非零值和非空可迭代对象的真值都是相当直观的True</li>
</ul></li>
</ul>
<p>dataframe转置 - .T</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#导入数据</span></span><br><span class="line">df1 = pd.DataFrame(&#123;<span class="string">&quot;a&quot;</span>: [<span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span>], <span class="string">&quot;b&quot;</span>: [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>]&#125;, dtype=<span class="built_in">bool</span>)</span><br><span class="line">df2 = pd.DataFrame(&#123;<span class="string">&quot;a&quot;</span>: [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>], <span class="string">&quot;b&quot;</span>: [<span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>]&#125;, dtype=<span class="built_in">bool</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df1</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df2</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df1 ^ df2</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#  &amp;当且仅当A B都为真,才为真,否则false &quot;且&quot;</span></span><br><span class="line">df1&amp;df2</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#  | 至少一真为真,否则false &quot;或&quot;</span></span><br><span class="line">df1 | df2</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#导入df</span></span><br><span class="line">df = pd.DataFrame(np.random.randn(<span class="number">10</span>, <span class="number">4</span>), columns=[<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>, <span class="string">&quot;D&quot;</span>])</span><br><span class="line">df.iloc[<span class="number">1</span>,<span class="number">1</span>]=<span class="literal">None</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">(df&gt;<span class="number">0</span>).<span class="built_in">all</span>()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">(df&gt;<span class="number">0</span>).<span class="built_in">any</span>().<span class="built_in">any</span>()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.T</span><br></pre></td></tr></table></figure>
<h3 id="数据描述统计">数据描述统计</h3>
<h4 id="直观系统描述">直观系统描述</h4>
<ul>
<li>. info 获取# Column Non-Null Count Dtype</li>
<li><code>print(df.iloc[-20:, :12]&lt;span class="mark"&gt;.to_string()&lt;/span&gt;)</code>
以表格形式返回 DataFrame 的字符串表示形式
<ul>
<li>通过设置以下选项来更改在单行上打印的量：<code>display.width</code>
<ul>
<li><code>pd.set_option("display.width", 40)</code> # default is 80</li>
<li><code>pd.set_option("display.max_colwidth", 30)</code>#设置display.max_colwidth</li>
</ul></li>
</ul></li>
<li>describe包括汇总数据集分布的中心趋势、色散和形状（不包括值）的统计量
<ul>
<li>df.describe()</li>
<li>df.describe(include=[np.number])</li>
<li><a
target="_blank" rel="noopener" href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.describe.html#pandas.DataFrame.describe%5D">describe详细讲解在此处</a></li>
</ul></li>
</ul>
<p>DataFrame 列属性访问</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df = pd.DataFrame(&#123;<span class="string">&quot;foo1&quot;</span>: np.random.randn(<span class="number">5</span>), <span class="string">&quot;foo2&quot;</span>: np.random.randn(<span class="number">5</span>)&#125;)</span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#访问某一列</span></span><br><span class="line">df.foo1</span><br></pre></td></tr></table></figure>
<p>访问头部和尾部 - head（） 和 tail（） 方法,默认显示5</p>
<h4 id="细节统计描述">细节统计描述</h4>
<p>DataFrame上存在大量用于计算描述性统计和其他相关操作的方法。 -
其中大多数是聚合（因此产生较低维的结果），如 sum（）、mean（） 和
quantile（） + 但其中一些（如 cumsum（） 和
cumprod（））会产生相同大小的对象 - 返回系列：无需轴参数 默认 axis=0
按列计算，如果axis=1,那就相当于按行了，也可以省略axis,只写0或1或什么都不写
- 返回数据帧：要轴参数“索引”（轴=0，默认值）、“列”（轴=1） +
所有此类方法都有一个选项 默认 skipna=True 是否排除缺失数据</p>
<table>
<thead>
<tr class="header">
<th>功能</th>
<th>描述</th>
<th>功能</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>count</code></td>
<td>非 NA 观测值的数量</td>
<td><code>skew</code></td>
<td>样品偏度（第 3 力矩）</td>
</tr>
<tr class="even">
<td><code>sum</code></td>
<td>值的总和</td>
<td><code>kurt</code></td>
<td>样品峰度（第 4 个力矩）</td>
</tr>
<tr class="odd">
<td><code>mean</code></td>
<td>均值</td>
<td><code>quantile</code></td>
<td>样本分位数（值为 %）</td>
</tr>
<tr class="even">
<td><code>mad</code></td>
<td>平均绝对偏差</td>
<td><code>cumsum</code></td>
<td>累计总和</td>
</tr>
<tr class="odd">
<td><code>median</code></td>
<td>值的算术中位数</td>
<td><code>cumprod</code></td>
<td>累计乘</td>
</tr>
<tr class="even">
<td><code>min</code></td>
<td>最低</td>
<td><code>cummax</code></td>
<td>累计最大值</td>
</tr>
<tr class="odd">
<td><code>max</code></td>
<td>最大</td>
<td><code>cummin</code></td>
<td>累计最小值</td>
</tr>
<tr class="even">
<td><code>mode</code></td>
<td>模式</td>
<td><code>sem</code></td>
<td>均值的标准误差</td>
</tr>
<tr class="odd">
<td><code>abs</code></td>
<td>绝对值</td>
<td><code>var</code></td>
<td>无偏方差</td>
</tr>
<tr class="even">
<td><code>prod</code></td>
<td>价值的乘积</td>
<td><code>std</code></td>
<td>样品标准偏差</td>
</tr>
<tr class="odd">
<td><code>idxmax</code></td>
<td>最大值对应索引</td>
<td><code>idxmin</code></td>
<td>最小值对应索引</td>
</tr>
</tbody>
</table>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#导入数据</span></span><br><span class="line">df = pd.DataFrame(&#123;<span class="string">&quot;foo1&quot;</span>: np.random.randn(<span class="number">5</span>), <span class="string">&quot;foo2&quot;</span>: np.random.randn(<span class="number">5</span>)&#125;)</span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.mean(<span class="number">0</span>) <span class="comment">#按列导入</span></span><br><span class="line">df.mean(<span class="number">1</span>) <span class="comment">#按行导入</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#对变量标准化，并且求变量标准差</span></span><br><span class="line">ts_stand = (df - df.mean()) / df.std()</span><br><span class="line">ts_stand.std()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#### 拓展描述性统计包</span></span><br><span class="line">- pandas profiling</span><br></pre></td></tr></table></figure>
<h2 id="第二章pandas的基础功能">第二章pandas的基础功能</h2>
<h3 id="dataframe矩阵运算">dataframe矩阵运算</h3>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<p>DataFrame具有 -
add（），sub（），mul（），div（）和相关函数radd（），rsub（），...用于执行二进制操作
- add表示其他列(行)都加上这一列(行)数据, - sub(a,axis=0或1)
表示其他列(行)都减这一列(行)数据 - 都有参数fill_value=0
表示空缺值以零填充， -
<mark>值得注意，空值以零填补，填补的是运算前那组数据，对于，初始数据A不会产生影响
- 并且空值NaN加任何数都等于空值Nan</mark>
空值不能用于加减乘除，不能用于逻辑判断
-都有参数level=0（位置或者填index的name），表示基于几索引做变换 其他同理
- 具有 + -等全局运算规则 - 比如df-[1,2,3]，代表每一行都减[1,2,3]</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">index = pd.date_range(<span class="string">&quot;1/1/2000&quot;</span>, periods=<span class="number">8</span>)</span><br><span class="line">s = pd.Series(np.random.randn(<span class="number">5</span>), index=[<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>, <span class="string">&quot;e&quot;</span>])</span><br><span class="line">df = pd.DataFrame(np.random.randn(<span class="number">8</span>, <span class="number">3</span>), index=index, columns=[<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>])</span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#两者等价</span></span><br><span class="line">df.sub(df.iloc[<span class="number">1</span>], axis=<span class="number">1</span>)</span><br><span class="line">df.sub(df.iloc[<span class="number">1</span>], axis=<span class="string">&quot;columns&quot;</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#两者等价</span></span><br><span class="line">df.sub(df.A,axis=<span class="number">0</span>)</span><br><span class="line">df.sub(df.A,axis=<span class="string">&#x27;index&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h4
id="将多索引数据帧的级别与序列对齐">将多索引数据帧的级别与序列对齐</h4>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df = pd.DataFrame(</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="string">&quot;one&quot;</span>: pd.Series(np.random.randn(<span class="number">3</span>), index=[<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>]),</span><br><span class="line">        <span class="string">&quot;two&quot;</span>: pd.Series(np.random.randn(<span class="number">4</span>), index=[<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>]),</span><br><span class="line">        <span class="string">&quot;three&quot;</span>: pd.Series(np.random.randn(<span class="number">3</span>), index=[<span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>]),</span><br><span class="line">    &#125;</span><br><span class="line">)</span><br><span class="line">dfmi = df.copy()</span><br><span class="line"></span><br><span class="line">dfmi.index = pd.MultiIndex.from_tuples(</span><br><span class="line">    [(<span class="number">1</span>, <span class="string">&quot;a&quot;</span>), (<span class="number">1</span>, <span class="string">&quot;b&quot;</span>), (<span class="number">1</span>, <span class="string">&quot;c&quot;</span>), (<span class="number">2</span>, <span class="string">&quot;a&quot;</span>)], names=[<span class="string">&quot;first&quot;</span>, <span class="string">&quot;second&quot;</span>]</span><br><span class="line">)</span><br><span class="line">dfmi.sub(df[<span class="string">&quot;two&quot;</span>], axis=<span class="number">0</span>, level=<span class="string">&quot;second&quot;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="比较对象是否等效">比较对象是否等效</h4>
<pre><code>- 易错点，NaN不能进行逻辑判断，所以可能逻辑判断有专门的的函数 `A.equals(B)`
- equals可以忽略NaN结果,但要求类型也想同
- 而== 判断可以判断不同类型，但内容相同的对象</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#引入数据</span></span><br><span class="line">index = pd.date_range(<span class="string">&quot;1/1/2000&quot;</span>, periods=<span class="number">8</span>)</span><br><span class="line">s = pd.Series(np.random.randn(<span class="number">5</span>), index=[<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>, <span class="string">&quot;e&quot;</span>])</span><br><span class="line">df = pd.DataFrame(np.random.randn(<span class="number">8</span>, <span class="number">3</span>), index=index, columns=[<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>])</span><br><span class="line">df.iloc[<span class="number">1</span>,<span class="number">1</span>]=<span class="literal">None</span></span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">(df + df == df * <span class="number">2</span>).<span class="built_in">all</span>().<span class="built_in">all</span>()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">(df + df).equals(df * <span class="number">2</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">(pd.Series([<span class="literal">None</span>, <span class="string">&quot;bar&quot;</span>, <span class="string">&quot;baz&quot;</span>])) .equals(pd.Series([<span class="literal">None</span>, <span class="string">&quot;bar&quot;</span>, <span class="string">&quot;baz&quot;</span>])) </span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">pd.Series([<span class="string">&quot;foo&quot;</span>, <span class="string">&quot;bar&quot;</span>, <span class="string">&quot;baz&quot;</span>]) == pd.Index([<span class="string">&quot;foo&quot;</span>, <span class="string">&quot;bar&quot;</span>, <span class="string">&quot;baz&quot;</span>])</span><br></pre></td></tr></table></figure>
<h4 id="合并重叠的数据集">合并重叠的数据集</h4>
<p>用数据集B的数据，来填补数据集A的NaN值 - 用combine()函数 -
<code>combine（other， func， fill_value=None， overwrite=True)</code> -
<mark>fill_value 在将列传递给合并函数之前填充 Nones</mark> -
<mark>但如果两个数据帧中的同一元素为 None，则保留 None</mark> -
overwrite，默认为 True - 如果为 True，则 self 中不存在于其他列的列将被
NaN 覆盖</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#导入数据</span></span><br><span class="line">df1 = pd.DataFrame(</span><br><span class="line">    &#123;<span class="string">&quot;A&quot;</span>: [<span class="number">1.0</span>, np.nan, <span class="number">3.0</span>, <span class="number">5.0</span>, np.nan], <span class="string">&quot;B&quot;</span>: [np.nan, <span class="number">2.0</span>, <span class="number">3.0</span>, np.nan, <span class="number">6.0</span>]&#125;</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">df2 = pd.DataFrame(</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="string">&quot;A&quot;</span>: [<span class="number">5.0</span>, <span class="number">2.0</span>, <span class="number">4.0</span>, np.nan, <span class="number">3.0</span>, <span class="number">7.0</span>],</span><br><span class="line">        <span class="string">&quot;B&quot;</span>: [np.nan, np.nan, <span class="number">3.0</span>, <span class="number">4.0</span>, <span class="number">6.0</span>, <span class="number">8.0</span>],</span><br><span class="line">    &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#用数据集B的数据，来填补数据集A的NaN值 </span></span><br><span class="line">df1.combine(df2, <span class="keyword">lambda</span> x,y:np.where(pd.isna(x),y,x))</span><br><span class="line"><span class="comment">#np.where(condition,x,y) 当where内有三个参数时，第一个参数表示条件，当条件成立时where方法返回x，当条件不成立时where返回y</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#使用选择较小值的简单函数进行组合</span></span><br><span class="line">df1.combine(df2, <span class="keyword">lambda</span> x,y:np.where(x&gt;y,y,x),fill_value=<span class="number">0</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#使用选择较大值的简单函数进行组合</span></span><br><span class="line">df1.combine(df2, <span class="keyword">lambda</span> x,y:np.where(x&gt;y,x,y),fill_value=<span class="number">0</span>)</span><br></pre></td></tr></table></figure>
<h3 id="直方图表">直方图表</h3>
<h4 id="series直方图表">series直方图表</h4>
<p>Series.value_counts(normalize=False, sort=True, ascending=False,
bins=None, dropna=True) - normalize设置为 True
时，通过将所有值除以值的总和来返回相对频率 - bin
从连续变量变为类别变量，bin输入分类的数量 - dropna是否记录空值 -
ascending升降序</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s = pd.Series([<span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, np.nan])</span><br><span class="line">s.value_counts(normalize=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>
<h4 id="dataframe-直方图表">dataframe 直方图表</h4>
<p>DataFrame.value_counts(subset=None, normalize=False, sort=True,
ascending=False, dropna=True) - sort=True 是否按频率排序 - 可选子集</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#导入数据</span></span><br><span class="line">df = pd.DataFrame(&#123;<span class="string">&#x27;num_legs&#x27;</span>: [<span class="number">2</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">6</span>],</span><br><span class="line">                   <span class="string">&#x27;num_wings&#x27;</span>: [<span class="number">2</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]&#125;,</span><br><span class="line">                  index=[<span class="string">&#x27;falcon&#x27;</span>, <span class="string">&#x27;dog&#x27;</span>, <span class="string">&#x27;cat&#x27;</span>, <span class="string">&#x27;ant&#x27;</span>])</span><br><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.value_counts()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.value_counts(subset=[<span class="string">&#x27;num_legs&#x27;</span>])</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.value_counts(ascending=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>
<h3 id="与函数相结合">与函数相结合</h3>
<p>按表函数应用：管道（）</p>
<p>行或列函数应用程序：apply（）</p>
<p>聚合 API：agg（） 和 transform（）</p>
<p>应用 Elementwise Functions： applymap（）</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">extract_city_name</span>(<span class="params">df</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    Chicago, IL -&gt; Chicago for city_name column</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    df[<span class="string">&quot;city_name&quot;</span>] = df[<span class="string">&quot;city_and_code&quot;</span>].<span class="built_in">str</span>.split(<span class="string">&quot;,&quot;</span>).<span class="built_in">str</span>.get(<span class="number">0</span>)</span><br><span class="line">    <span class="keyword">return</span> df</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add_country_name</span>(<span class="params">df, country_name=<span class="literal">None</span></span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    Chicago -&gt; Chicago-US for city_name column</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    col = <span class="string">&quot;city_name&quot;</span></span><br><span class="line">    df[<span class="string">&quot;city_and_country&quot;</span>] = df[col] + country_name</span><br><span class="line">    <span class="keyword">return</span> df</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">df_p = pd.DataFrame(&#123;<span class="string">&quot;city_and_code&quot;</span>: [<span class="string">&quot;Chicago, IL&quot;</span>],</span><br><span class="line">                    <span class="string">&quot;two&quot;</span>:[<span class="string">&#x27;a&#x27;</span>]</span><br><span class="line">                    &#125;</span><br><span class="line">                   )</span><br><span class="line">df_p</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">add_country_name(extract_city_name(df_p), country_name=<span class="string">&quot;US&quot;</span>)<span class="comment">### 第一种写法</span></span><br><span class="line">df_p.pipe(extract_city_name).pipe(add_country_name, country_name=<span class="string">&quot;US&quot;</span>)<span class="comment">###python更支持第二种写法</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">tsdf = pd.DataFrame(</span><br><span class="line">    np.random.randn(<span class="number">1000</span>, <span class="number">3</span>),</span><br><span class="line">    columns=[<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>],</span><br><span class="line">    index=pd.date_range(<span class="string">&quot;1/1/2000&quot;</span>, periods=<span class="number">1000</span>),</span><br><span class="line">)</span><br><span class="line">tsdf</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">tsdf.apply(<span class="keyword">lambda</span> x:x.idxmax())</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">函数`drop_duplicates`关键参数</span><br><span class="line">- keep=</span><br><span class="line">    - first只保留第一次出现的行</span><br><span class="line">    - last 只保留最后一次出现的行</span><br><span class="line">    - <span class="literal">False</span> 删除所有重复行</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#方法链小剧场，求一共有多少学校多少年级，并展示前五项，因为value_count后有索引，所以得将多索引转化为多列</span></span><br><span class="line"><span class="comment">#value_counts来求，和drop_duplicates一个很大的区别就是drop_duplicates保留了其他列数据，但value_count没有保留</span></span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line">df = pd.read_csv(<span class="string">&#x27;jupyter学习数据/learn_pandas.csv&#x27;</span>) </span><br><span class="line">df_demo = df.copy</span><br><span class="line">(</span><br><span class="line">df.value_counts(subset=[<span class="string">&#x27;School&#x27;</span>, <span class="string">&#x27;Grade&#x27;</span>]).</span><br><span class="line">pipe(pd.DataFrame,columns=[<span class="string">&#x27;学校名&#x27;</span>]).</span><br><span class="line">reset_index([<span class="string">&#x27;School&#x27;</span>, <span class="string">&#x27;Grade&#x27;</span>]).tail()</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#方法二，用drop_duplicate</span></span><br><span class="line">df.drop_duplicates(subset=[<span class="string">&#x27;School&#x27;</span>,<span class="string">&#x27;Grade&#x27;</span>],keep=<span class="string">&#x27;first&#x27;</span>,ignore_index=<span class="literal">True</span>).tail()</span><br></pre></td></tr></table></figure>
<h3 id="数据读取和写入">数据读取和写入</h3>
<h4 id="数据读取">数据读取</h4>
<p>有一些常用的公共参数 - header=None 第一行不做列名 - index_col
某一列或几列作为索引 - usecols=[] 选子集哪些列 - parse_datas
需要转化为时间的列 - nrows 表示读取的数据行数</p>
<p>读取 txt 文件时，经常遇到分隔符非空格的情况<span class="mark"></p>
<p>read_table 有一个分割参数 sep</span>, - sep用正则查询 -
用sep一定额外指定python引擎,即engine='python',否则会报错.</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line">df_csv=pd.read_csv(<span class="string">&#x27;jupyter学习数据/my_csv.csv&#x27;</span>,usecols=[<span class="string">&#x27;col2&#x27;</span>])</span><br><span class="line">df_csv</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_txt=pd.read_table(<span class="string">&#x27;jupyter学习数据/my_table.txt&#x27;</span>)</span><br><span class="line">df_txt</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_excel=pd.read_excel(<span class="string">&#x27;jupyter学习数据/my_excel.xlsx&#x27;</span>)</span><br><span class="line">df_excel</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_txtsep=pd.read_table(<span class="string">&#x27;jupyter学习数据/my_table_special_sep.txt&#x27;</span>,sep=<span class="string">&#x27;\|\|\|\|&#x27;</span>,engine=<span class="string">&#x27;python&#x27;</span>)</span><br><span class="line">df_txtsep</span><br></pre></td></tr></table></figure>
<h4 id="数据写入">数据写入</h4>
<p>数据写入中，最常用的操作是把 index 设置为 False
，特别当索引没有特殊意义的时候，这样的行为 能把索引在保存的时候去除</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_csv.to_csv(<span class="string">&#x27;jupyter学习数据/my_csv_saved.csv&#x27;</span>,index=<span class="literal">False</span>)</span><br><span class="line">df_excel.to_csv(<span class="string">&#x27;jupyter学习数据/my_txt_saved.txt&#x27;</span>,sep=<span class="string">&#x27;\t&#x27;</span>,index=<span class="literal">False</span>)</span><br></pre></td></tr></table></figure>
<p>把表格快速转换为 markdown 和 latex 语言，可以使用 to_markdown 和
to_latex 函数</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="built_in">print</span>(df_excel.to_markdown())</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment"># print(df_excel.to_latex())过时了,不推荐</span></span><br><span class="line"><span class="built_in">print</span>(df_excel.style.to_latex())</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">!pip install --user pandas_profiling --no-warn-script-location</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> pandas_profiling</span><br><span class="line">data_fifa = pd.read_excel(<span class="string">&#x27;jupyter学习数据/my_excel.xlsx&#x27;</span>)</span><br><span class="line">profile = data_fifa.profile_report(title=<span class="string">&#x27;Pandas Profiling Report&#x27;</span>)</span><br><span class="line">profile.to_file(output_file=<span class="string">&quot;fifa_pandas_profiling.html&quot;</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas_profiling</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">### 常用函数</span></span><br></pre></td></tr></table></figure>
<h4 id="替换函数对原列再编码">替换函数，对原列再编码</h4>
<p>pandas 中的替换函数可以归纳为三类：映射替换、逻辑替换、数值替换。</p>
<p>其中映射替换包含 replace 方法、</p>
<p>第八章中的 str.replace 方法</p>
<p>以及第九章中的 cat.codes 方法，此处介绍 replace 的用法</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#导入数据</span></span><br><span class="line">df = pd.read_csv(<span class="string">&#x27;jupyter学习数据/learn_pandas.csv&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h5 id="replace">replace</h5>
<p>replace
中，可以通过<mark>字典构造</mark>，或者<mark>传入两个列表</mark>来进行替换
- method - ffill用前面一个最近未被替换的值进行替换 -
bfill使用后面最近未被替换的值替换 - 例子 -
<code>df.replace(&#123;'a': 0, 'b': 5&#125;,100)</code>把列名a中的value=0的以及b中的value=5的转化为100
-
<code>series.replace(1, method='ffill')</code>把序列中值为1的值按照向前赋值进行replace</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#最简单的用法，全局把零换成5</span></span><br><span class="line">df.replace(<span class="number">0</span>, <span class="number">5</span>).head()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df[<span class="string">&#x27;Gender&#x27;</span>].head()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df[<span class="string">&#x27;Gender&#x27;</span>].replace(&#123;<span class="string">&#x27;Female&#x27;</span>:<span class="number">0</span>, <span class="string">&#x27;Male&#x27;</span>:<span class="number">1</span>&#125;).head()</span><br></pre></td></tr></table></figure>
<p>传入两个列表来进行替换</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df[<span class="string">&#x27;Gender&#x27;</span>].replace([<span class="string">&#x27;Female&#x27;</span>, <span class="string">&#x27;Male&#x27;</span>], [<span class="number">0</span>, <span class="number">1</span>]).head()</span><br></pre></td></tr></table></figure>
<p>传入一个列表+指定的method来recode</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s = pd.Series([<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="string">&#x27;a&#x27;</span>])</span><br><span class="line">s.replace(<span class="number">1</span>, method=<span class="string">&#x27;bfill&#x27;</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.replace(&#123;<span class="string">&#x27;Test_Number&#x27;</span>: &#123;<span class="number">1</span>:np.nan&#125;&#125;).replace(&#123;<span class="string">&#x27;Test_Number&#x27;</span>: &#123;<span class="number">2</span>:np.nan&#125;&#125;).replace(&#123;<span class="string">&#x27;Test_Number&#x27;</span>: &#123;<span class="number">3</span>:np.nan&#125;&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.replace(&#123;<span class="string">&#x27;Test_Number&#x27;</span>:<span class="number">1</span>&#125;,<span class="string">&#x27;a&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h5 id="where与mask-逻辑替换函数">where与mask 逻辑替换函数</h5>
<p>这两个函数是完全对称的： - where 函数在传入条件为 False 的对应行进行
替换， - 而 mask 在传入条件为 True
的对应行进行替换，当不指定替换值时，替换为缺失值。</p>
<p>接下来主要讲mask函数 - mask函数也支持传入布尔series当条件</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s = pd.Series([-<span class="number">1</span>, <span class="number">1.2345</span>, <span class="number">100</span>, -<span class="number">50</span>])</span><br><span class="line">s.where(s&lt;<span class="number">0</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s.where(s&lt;<span class="number">0</span>, <span class="number">100</span>)</span><br></pre></td></tr></table></figure>
<h5 id="round-abs-clip-方法">round, abs, clip 方法</h5>
<p>它们分别表示取整、取绝对值和截断 - s.round(2) - s.clip(0, 2)
前两个数分别表示上下截断边界 - 在 clip
中，超过边界的只能截断为边界值</p>
<h4 id="排序函数">排序函数</h4>
<p>排序共有两种方式， - 其一为值排序，其二为索引排序，、 - 对应的函数是
<mark>sort_values</mark> 和 <mark>sort_index</mark> 。 - 默认参数
ascending=True 为升序： -
多列排序的问题，比如在体重相同的情况下，对身高进行排序，并且保持身高降序排列
-
<code>df_demo.sort_values(['Weight','Height'],ascending=[True,False]).head()</code></p>
<p>体重升序排列 为了演示排序函数，下面先利用 set_index
方法把年级和姓名两列作为索引，</p>
<p>多级索引的内容和索引设置的方法将在第三章讲解</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#导入数据</span></span><br><span class="line">df = pd.read_csv(<span class="string">&#x27;jupyter学习数据/learn_pandas.csv&#x27;</span>)</span><br><span class="line">df_demo = df[[<span class="string">&#x27;Grade&#x27;</span>, <span class="string">&#x27;Name&#x27;</span>, <span class="string">&#x27;Height&#x27;</span>, <span class="string">&#x27;Weight&#x27;</span>]].set_index([<span class="string">&#x27;Grade&#x27;</span>,<span class="string">&#x27;Name&#x27;</span>])</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_demo.sort_values(<span class="string">&#x27;Height&#x27;</span>,ascending=<span class="literal">False</span>).head()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_demo.sort_values([<span class="string">&#x27;Weight&#x27;</span>,<span class="string">&#x27;Height&#x27;</span>],ascending=[<span class="literal">True</span>,<span class="literal">False</span>]).head()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<h5 id="where与mask-逻辑替换函数-1">where与mask 逻辑替换函数</h5>
<p>这两个函数是完全对称的： - where 函数在传入条件为 False 的对应行进行
替换， - 而 mask 在传入条件为 True
的对应行进行替换，当不指定替换值时，替换为缺失值。</p>
<p>接下来主要讲mask函数 - mask函数也支持传入布尔series当条件</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s = pd.Series([-<span class="number">1</span>, <span class="number">1.2345</span>, <span class="number">100</span>, -<span class="number">50</span>])</span><br><span class="line">s.where(s&lt;<span class="number">0</span>)</span><br></pre></td></tr></table></figure>
<pre><code>0    -1.0
1     NaN
2     NaN
3   -50.0
dtype: float64</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">s.where(s&lt;<span class="number">0</span>, <span class="number">100</span>)</span><br></pre></td></tr></table></figure>
<pre><code>0     -1.0
1    100.0
2    100.0
3    -50.0
dtype: float64</code></pre>
<h5 id="round-abs-clip-方法-1">round, abs, clip 方法</h5>
<p>它们分别表示取整、取绝对值和截断 - s.round(2) - s.clip(0, 2)
前两个数分别表示上下截断边界 - 在 clip
中，超过边界的只能截断为边界值</p>
<h4 id="排序函数-1">排序函数</h4>
<p>排序共有两种方式， - 其一为值排序，其二为索引排序，、 - 对应的函数是
<mark>sort_values</mark> 和 <mark>sort_index</mark> 。 - 默认参数
ascending=True 为升序： -
多列排序的问题，比如在体重相同的情况下，对身高进行排序，并且保持身高降序排列
-
<code>df_demo.sort_values(['Weight','Height'],ascending=[True,False]).head()</code></p>
<p>体重升序排列 为了演示排序函数，下面先利用 set_index
方法把年级和姓名两列作为索引，</p>
<p>多级索引的内容和索引设置的方法将在第三章讲解</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#导入数据</span></span><br><span class="line">df = pd.read_csv(<span class="string">&#x27;jupyter学习数据/learn_pandas.csv&#x27;</span>)</span><br><span class="line">df_demo = df[[<span class="string">&#x27;Grade&#x27;</span>, <span class="string">&#x27;Name&#x27;</span>, <span class="string">&#x27;Height&#x27;</span>, <span class="string">&#x27;Weight&#x27;</span>]].set_index([<span class="string">&#x27;Grade&#x27;</span>,<span class="string">&#x27;Name&#x27;</span>])</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df_demo.sort_values(<span class="string">&#x27;Height&#x27;</span>,ascending=<span class="literal">False</span>).head()</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://yang1he.gitee.io">杨一赫</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://yang1he.gitee.io/2021/09/05/pandas%E5%85%A5%E9%97%A8/">http://yang1he.gitee.io/2021/09/05/pandas%E5%85%A5%E9%97%A8/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://yang1he.gitee.io" target="_blank">杨一赫的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E7%A0%81%E5%86%9C%E5%9F%BA%E7%A1%80/">-码农基础</a></div><div class="post_share"><div class="social-share" data-image="https://img.freepik.com/premium-psd/plastic-individual-tablecloth-mockup-top-view_1332-10763.jpg?w=1480" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-full"><a href="/2022/07/05/%E5%A6%82%E4%BD%95%E7%88%AC%E5%8F%96%E7%9F%A5%E4%B9%8Elive/"><img class="prev-cover" src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220613155253825.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">如何爬取知乎live-2022秋</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/favicon2.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">杨一赫</div><div class="author-info__description">阳光开朗大男孩</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/yang1he"><i class="fab fa-github"></i><span>gitee</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">平平无奇的网站</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E7%AB%A0"><span class="toc-number">1.</span> <span class="toc-text">第一章</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AE%80%E4%BB%8B"><span class="toc-number">1.1.</span> <span class="toc-text">数据结构简介</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#series%E5%88%9B%E5%BB%BA"><span class="toc-number">1.2.</span> <span class="toc-text">series创建</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%8E%E6%95%B0%E7%BB%84%E4%B8%AD%E5%88%9B%E5%BB%BA"><span class="toc-number">1.2.1.</span> <span class="toc-text">从数组中创建</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#series-%E5%8F%AF%E4%BB%A5%E4%BB%8E%E5%AD%97%E5%85%B8%E4%B8%AD%E5%AE%9E%E4%BE%8B%E5%8C%96"><span class="toc-number">1.2.2.</span> <span class="toc-text">series 可以从字典中实例化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%8E%E5%8D%95%E4%B8%AA%E6%95%B0%E5%80%BC%E5%88%9B%E5%BB%BAseries"><span class="toc-number">1.2.3.</span> <span class="toc-text">从单个数值创建series</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95%E7%9A%84%E5%88%87%E7%89%87%E6%93%8D%E4%BD%9C"><span class="toc-number">1.2.4.</span> <span class="toc-text">索引的切片操作</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#series-%E7%9F%A2%E9%87%8F%E5%8C%96%E6%93%8D%E4%BD%9C"><span class="toc-number">1.2.5.</span> <span class="toc-text">series 矢量化操作</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#series%E7%9A%84%E5%90%8D%E7%A7%B0%E5%B1%9E%E6%80%A7name"><span class="toc-number">1.2.6.</span> <span class="toc-text">series的名称属性name</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#dataframe%E5%88%9B%E5%BB%BA"><span class="toc-number">1.3.</span> <span class="toc-text">dataframe创建</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E4%BA%8E%E5%AD%97%E5%85%B8%E7%94%9F%E6%88%90dataframe"><span class="toc-number">1.3.1.</span> <span class="toc-text">基于字典生成DataFrame</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dataframe%E6%9F%A5%E8%AF%A2index%E5%92%8Ccolums"><span class="toc-number">1.3.2.</span> <span class="toc-text">dataframe查询index和colums</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%8E%E5%88%97%E8%A1%A8%E6%88%96%E8%80%85ndarray"><span class="toc-number">1.3.3.</span> <span class="toc-text">从列表或者ndarray</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%8E%E5%88%97%E8%A1%A8%E5%AD%97%E5%85%B8%E5%AF%BC%E5%85%A5"><span class="toc-number">1.3.4.</span> <span class="toc-text">从列表字典导入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A4%9A%E7%B4%A2%E5%BC%95%E6%A1%86%E6%9E%B6__%E4%BB%8E%E5%85%83%E7%A5%96%E5%AD%97%E5%85%B8%E4%B8%AD%E5%AF%BC%E5%85%A5"><span class="toc-number">1.3.5.</span> <span class="toc-text">多索引框架__从元祖字典中导入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A4%87%E7%94%A8%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">1.3.6.</span> <span class="toc-text">备用构造函数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%97%E9%80%89%E6%8B%A9%E5%88%A0%E9%99%A4%E6%B7%BB%E5%8A%A0"><span class="toc-number">1.4.</span> <span class="toc-text">列选择,删除,添加</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#dataframe%E7%9A%84assign%E6%96%B9%E6%B3%95"><span class="toc-number">1.4.1.</span> <span class="toc-text">DataFrame的assign方法()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95%E9%80%89%E6%8B%A9"><span class="toc-number">1.4.2.</span> <span class="toc-text">索引选择</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dataframe%E4%B9%8B%E9%97%B4%E5%8A%A0%E5%87%8F%E5%90%88%E5%B9%B6"><span class="toc-number">1.4.3.</span> <span class="toc-text">dataframe之间加减合并</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95%E9%80%89%E6%8B%A9-1"><span class="toc-number">1.4.4.</span> <span class="toc-text">索引选择</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dataframe%E4%B9%8B%E9%97%B4%E5%8A%A0%E5%87%8F%E5%90%88%E5%B9%B6-1"><span class="toc-number">1.4.5.</span> <span class="toc-text">dataframe之间加减合并</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dataframe%E6%94%AF%E6%8C%81%E6%A0%87%E9%87%8F%E5%B8%83%E5%B0%94%E8%BF%90%E7%AE%97%E8%BD%AC%E7%BD%AE"><span class="toc-number">1.4.6.</span> <span class="toc-text">dataframe支持标量,布尔运算,转置</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E6%8F%8F%E8%BF%B0%E7%BB%9F%E8%AE%A1"><span class="toc-number">1.5.</span> <span class="toc-text">数据描述统计</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%9B%B4%E8%A7%82%E7%B3%BB%E7%BB%9F%E6%8F%8F%E8%BF%B0"><span class="toc-number">1.5.1.</span> <span class="toc-text">直观系统描述</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%86%E8%8A%82%E7%BB%9F%E8%AE%A1%E6%8F%8F%E8%BF%B0"><span class="toc-number">1.5.2.</span> <span class="toc-text">细节统计描述</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC%E4%BA%8C%E7%AB%A0pandas%E7%9A%84%E5%9F%BA%E7%A1%80%E5%8A%9F%E8%83%BD"><span class="toc-number">2.</span> <span class="toc-text">第二章pandas的基础功能</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#dataframe%E7%9F%A9%E9%98%B5%E8%BF%90%E7%AE%97"><span class="toc-number">2.1.</span> <span class="toc-text">dataframe矩阵运算</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B0%86%E5%A4%9A%E7%B4%A2%E5%BC%95%E6%95%B0%E6%8D%AE%E5%B8%A7%E7%9A%84%E7%BA%A7%E5%88%AB%E4%B8%8E%E5%BA%8F%E5%88%97%E5%AF%B9%E9%BD%90"><span class="toc-number">2.1.1.</span> <span class="toc-text">将多索引数据帧的级别与序列对齐</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AF%94%E8%BE%83%E5%AF%B9%E8%B1%A1%E6%98%AF%E5%90%A6%E7%AD%89%E6%95%88"><span class="toc-number">2.1.2.</span> <span class="toc-text">比较对象是否等效</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%88%E5%B9%B6%E9%87%8D%E5%8F%A0%E7%9A%84%E6%95%B0%E6%8D%AE%E9%9B%86"><span class="toc-number">2.1.3.</span> <span class="toc-text">合并重叠的数据集</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%B4%E6%96%B9%E5%9B%BE%E8%A1%A8"><span class="toc-number">2.2.</span> <span class="toc-text">直方图表</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#series%E7%9B%B4%E6%96%B9%E5%9B%BE%E8%A1%A8"><span class="toc-number">2.2.1.</span> <span class="toc-text">series直方图表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dataframe-%E7%9B%B4%E6%96%B9%E5%9B%BE%E8%A1%A8"><span class="toc-number">2.2.2.</span> <span class="toc-text">dataframe 直方图表</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8E%E5%87%BD%E6%95%B0%E7%9B%B8%E7%BB%93%E5%90%88"><span class="toc-number">2.3.</span> <span class="toc-text">与函数相结合</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E8%AF%BB%E5%8F%96%E5%92%8C%E5%86%99%E5%85%A5"><span class="toc-number">2.4.</span> <span class="toc-text">数据读取和写入</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E8%AF%BB%E5%8F%96"><span class="toc-number">2.4.1.</span> <span class="toc-text">数据读取</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%86%99%E5%85%A5"><span class="toc-number">2.4.2.</span> <span class="toc-text">数据写入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9B%BF%E6%8D%A2%E5%87%BD%E6%95%B0%E5%AF%B9%E5%8E%9F%E5%88%97%E5%86%8D%E7%BC%96%E7%A0%81"><span class="toc-number">2.4.3.</span> <span class="toc-text">替换函数，对原列再编码</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#replace"><span class="toc-number">2.4.3.1.</span> <span class="toc-text">replace</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#where%E4%B8%8Emask-%E9%80%BB%E8%BE%91%E6%9B%BF%E6%8D%A2%E5%87%BD%E6%95%B0"><span class="toc-number">2.4.3.2.</span> <span class="toc-text">where与mask 逻辑替换函数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#round-abs-clip-%E6%96%B9%E6%B3%95"><span class="toc-number">2.4.3.3.</span> <span class="toc-text">round, abs, clip 方法</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8E%92%E5%BA%8F%E5%87%BD%E6%95%B0"><span class="toc-number">2.4.4.</span> <span class="toc-text">排序函数</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#where%E4%B8%8Emask-%E9%80%BB%E8%BE%91%E6%9B%BF%E6%8D%A2%E5%87%BD%E6%95%B0-1"><span class="toc-number">2.4.4.1.</span> <span class="toc-text">where与mask 逻辑替换函数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#round-abs-clip-%E6%96%B9%E6%B3%95-1"><span class="toc-number">2.4.4.2.</span> <span class="toc-text">round, abs, clip 方法</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8E%92%E5%BA%8F%E5%87%BD%E6%95%B0-1"><span class="toc-number">2.4.5.</span> <span class="toc-text">排序函数</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/06/24/An%20Intelligent%20Mobile%20Prediction%20method/" title="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/model_00.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks"/></a><div class="content"><a class="title" href="/2023/06/24/An%20Intelligent%20Mobile%20Prediction%20method/" title="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks">An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks</a><time datetime="2023-06-23T16:00:00.000Z" title="发表于 2023-06-24 00:00:00">2023-06-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/05/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E4%BD%A0%E4%B8%AA%E4%BA%BA%E8%B4%A6%E5%8F%B7%E7%9A%84openai%E7%9A%84api/" title="无题"><img src="/img/rick1.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="无题"/></a><div class="content"><a class="title" href="/2023/03/05/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E4%BD%A0%E4%B8%AA%E4%BA%BA%E8%B4%A6%E5%8F%B7%E7%9A%84openai%E7%9A%84api/" title="无题">无题</a><time datetime="2023-03-05T05:57:17.050Z" title="发表于 2023-03-05 13:57:17">2023-03-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/04/%E4%B8%89%E5%88%86%E9%92%9F4%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%9E%84%E5%BB%BAchatgpt%20webapp,%E6%94%AF%E6%8C%81%E9%AB%98%E5%B9%B6%E5%8F%91%E4%BB%A5%E5%8F%8A%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AF%B9%E8%AF%9D%E5%8A%9F%E8%83%BD/" title="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230305093941469.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能"/></a><div class="content"><a class="title" href="/2023/03/04/%E4%B8%89%E5%88%86%E9%92%9F4%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%9E%84%E5%BB%BAchatgpt%20webapp,%E6%94%AF%E6%8C%81%E9%AB%98%E5%B9%B6%E5%8F%91%E4%BB%A5%E5%8F%8A%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AF%B9%E8%AF%9D%E5%8A%9F%E8%83%BD/" title="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能">三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能</a><time datetime="2023-03-03T16:00:00.000Z" title="发表于 2023-03-04 00:00:00">2023-03-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/" title="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230303211538791.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码"/></a><div class="content"><a class="title" href="/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/" title="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码">多标签分类的CrossEntropyLoss到底需不需要One-Hot编码</a><time datetime="2023-03-02T16:00:00.000Z" title="发表于 2023-03-03 00:00:00">2023-03-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/02/28/GPTEX---%E4%B8%BAChatGPT%E8%80%8C%E7%94%9F%E7%9A%84latex%E8%BD%AF%E4%BB%B6/" title="GPTEX---为ChatGPT而生的latex软件"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230228145723183.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="GPTEX---为ChatGPT而生的latex软件"/></a><div class="content"><a class="title" href="/2023/02/28/GPTEX---%E4%B8%BAChatGPT%E8%80%8C%E7%94%9F%E7%9A%84latex%E8%BD%AF%E4%BB%B6/" title="GPTEX---为ChatGPT而生的latex软件">GPTEX---为ChatGPT而生的latex软件</a><time datetime="2023-02-27T16:00:00.000Z" title="发表于 2023-02-28 00:00:00">2023-02-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By 杨一赫</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="聊天"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.1
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container').forEach(node => {
            if (node.hasAttribute('display')) {
              btf.wrap(node, 'div', { class: 'mathjax-overflow' })
            } else {
              btf.wrap(node, 'span', { class: 'mathjax-overflow' })
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script><script>function addGitalkSource () {
  const ele = document.createElement('link')
  ele.rel = 'stylesheet'
  ele.href= 'https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.css'
  document.getElementsByTagName('head')[0].appendChild(ele)
}

function loadGitalk () {
  function initGitalk () {
    var gitalk = new Gitalk(Object.assign({
      clientID: '5a73aec03d8a0aeb971f',
      clientSecret: '54ad1c8adab95b382d582c73e9f597781de43525',
      repo: 'comment',
      owner: 'nmhjklnm',
      admin: ['nmhjklnm'],
      id: '07d539d04a64a07fe0b0b5f100116f2c',
      updateCountCallback: commentCount
    },null))

    gitalk.render('gitalk-container')
  }

  if (typeof Gitalk === 'function') initGitalk()
  else {
    addGitalkSource()
    getScript('https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.js').then(initGitalk)
  }
}

function commentCount(n){
  let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
  if (isCommentCount) {
    isCommentCount.innerHTML= n
  }
}

if ('Gitalk' === 'Gitalk' || !false) {
  if (false) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
  else loadGitalk()
} else {
  function loadOtherComment () {
    loadGitalk()
  }
}</script></div><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="//code.tidio.co/6zvu6bgfljrvvv9rw1y7hzehoriklr03.js" async="async"></script><script>function onTidioChatApiReady() {
  window.tidioChatApi.hide();
  window.tidioChatApi.on("close", function() {
    window.tidioChatApi.hide();
  });
}
if (window.tidioChatApi) {
  window.tidioChatApi.on("ready", onTidioChatApiReady);
} else {
  document.addEventListener("tidioChat-ready", onTidioChatApiReady);
}

var chatBtnFn = () => {
  document.getElementById("chat_btn").addEventListener("click", function(){
    window.tidioChatApi.show();
    window.tidioChatApi.open();
  });
}
chatBtnFn()
</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>